package com.enqualcomm.kids.mvp.voiceschedule;

import android.content.Context;
import android.os.Bundle;

import com.enqualcomm.kids.bean.VoiceSchedule;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechUnderstander;
import com.iflytek.cloud.SpeechUnderstanderListener;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.UnderstanderResult;


import org.json.JSONObject;

import common.utils.MyLogger;
import rx.Observable;
import rx.subjects.PublishSubject;

/**
 * Created by yinzhiqun on 2016/3/9.
 * 语义解析
 */
public class SpeechUnderstanderModel implements SpeechUnderstanderListener {
    SpeechUnderstander mSpeechUnderstander;
    private final PublishSubject<VoiceSchedule> subject;

    public SpeechUnderstanderModel(Context context) {
        SpeechUtility.createUtility(context, "appid=56d40e19");
        mSpeechUnderstander = SpeechUnderstander.createUnderstander(context, null);
        mSpeechUnderstander.setParameter(SpeechConstant.LANGUAGE, "zh_cn");// 设置语言
        mSpeechUnderstander.setParameter(SpeechConstant.ACCENT, "mandarin");//方言
        mSpeechUnderstander.setParameter(SpeechConstant.DOMAIN, "iat");
        mSpeechUnderstander.setParameter(SpeechConstant.NLP_VERSION, "2.0");
        mSpeechUnderstander.setParameter(SpeechConstant.RESULT_TYPE, "json");
        subject = PublishSubject.create();
    }

    public void start(){
        if(mSpeechUnderstander.isUnderstanding()){// 开始前检查状态
            mSpeechUnderstander.stopUnderstanding();
        }

        //开始语义解析
        mSpeechUnderstander.startUnderstanding(this);
    }

    public void stop(){
        mSpeechUnderstander.stopUnderstanding();
    }

    public void cancel(){
        mSpeechUnderstander.cancel();
    }

    public void destroy(){
        mSpeechUnderstander.destroy();
        subject.onCompleted();
    }

    @Override
    public void onVolumeChanged(int i, byte[] bytes) {
    }

    @Override
    public void onBeginOfSpeech() {
    }

    @Override
    public void onEndOfSpeech() {
    }

    @Override
    public void onResult(UnderstanderResult understanderResult) {
        String resultString = understanderResult.getResultString();
//        MyLogger.jLog().i(resultString);

        try {
            JSONObject jsonObject = new JSONObject(resultString);
            int rc = jsonObject.optInt("rc", -1);
            if (rc==0) {
                JSONObject slots = jsonObject.getJSONObject("semantic").getJSONObject("slots");
                JSONObject datetime = slots.getJSONObject("datetime");
                String date = datetime.getString("date");
                String time = datetime.getString("time");
                String content = slots.getString("content");
                //去掉结尾的句号
                if (content.endsWith("。")){
                    content = content.substring(0,content.length()-1);
                }

                String datetimeStr = date + " "+ time.substring(0,5);//只保留时，分

                subject.onNext(new VoiceSchedule(datetimeStr,content));
            }else{
                subject.onNext(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            subject.onNext(null);
        }
    }

    @Override
    public void onError(SpeechError speechError) {
        subject.onNext(null);
    }

    @Override
    public void onEvent(int i, int i1, int i2, Bundle bundle) {
    }

    public Observable<VoiceSchedule> onUnderstanderResult(){
        return subject.asObservable();
    }
}
